<?php
namespace app\composite;

class Company extends CompanyBase
{
    protected $item = [];

    function add(CompanyBase $company)
    {
        $nodeName = $company->getName();

        if (!isset($this->item[$nodeName])) {
            $this->item[$nodeName] = $company;
        } else {
            throw new Exception("该节点已存在,节点名称：" . $nodeName);
        }
    }

    function remove(CompanyBase $company)
    {
        $nodeName = $company->getName();

        if (isset($this->item[$nodeName])) {
            unset($this->item[$nodeName]);
        } else {
            throw new Exception("该节点不存在,节点名称：" . $nodeName);
        }
    }

    function show($deep = 0)
    {
        echo str_repeat('-', $deep) . $this->name;
        echo "<br>";
        foreach ($this->item as $value) {
            $value->show($deep + 4);
        }
    }

    function work($deep = 0)
    {
        foreach ($this->item as $value) {
            echo str_repeat('-', $deep) . "[{$this->name}]<br>";
            $value->work($deep + 2);
        }
    }
}